home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / ada / gwuada_8.zip / DOC.ZIP / FIBB.ADA next >
Text File  |  1993-08-17  |  2KB  |  60 lines

  1. --
  2. --    Program : Fibb.ada
  3. --    Purpose : This program calculates a fibbinochi number.  This number
  4. --        is calculated by summing all the numbers up to and including
  5. --        that number.  For example, 4 would be
  6. --
  7. --            1 + 2 + 3 + 4 = 10
  8. --
  9. --        It is similar to a factorial, but the growth of the number
  10. --        is much slower, and so we can use it with a much larger
  11. --        level of recursion.
  12. --
  13. --    To use with GWUMON : To use this program with GWUMON, from the command
  14. --              line type:
  15. --                       adacomp -a -b -mfibbinochi fibb.ada
  16. --                       gwumon -mfibbinochi
  17. --
  18. --    Take the default options on the first screen (speed = 6, exceptions = yes,
  19. --              and tasks = no) by hitting the "Esc" key.  Change from a Small
  20. --        Window to a Large Window on the second screen.
  21. --
  22. --    The purpose of this program is to show a large amount of recursion.
  23. --    Therefore, when prompted for the number to be calculated, choose 13.
  24. --    This will cause the program to recursively call Fibb_Calc 13 times.
  25. --    
  26. --    Notice that each time Fibb_Calc is called, a new window is opened until
  27. --    a total of 12 windows are opened.  At that point, no new windows are
  28. --    created, but notice that the highest level window "Scrolls" off to the
  29. --    upper right and is no longer visable.
  30. --
  31. --    When the program has gotten to the final window, notice how it begins
  32. --    to scrool the windows back on to the screen as the call stack unwinds.
  33. --    When all the programs on the stack are back on the screen, the stack
  34. --    continues to unwind, until there is only a single procedure left on
  35. --    the screen.  This shows the call stack, and the recursion of the program.
  36. --
  37. WITH Text_IO;
  38. PACKAGE My_Int_IO IS
  39.   NEW Text_IO.Integer_IO (Num => Integer);
  40.  
  41. WITH Text_IO; USE Text_IO;
  42. WITH My_Int_IO; USE My_Int_IO;
  43. PROCEDURE Fibbinochi IS
  44.     Temp : Integer; 
  45.     FUNCTION Fibb_Calc( Current_Num: IN Integer ) RETURN Integer is
  46.     BEGIN
  47.         IF Current_Num = 0
  48.         THEN
  49.             RETURN 0;
  50.         END IF;
  51.         RETURN Current_Num + Fibb_Calc( Current_Num - 1 );
  52.     END Fibb_Calc;
  53. BEGIN
  54.     PUT_LINE( "Enter the Fibbinochi number to be calculated" );
  55.     GET( Temp );
  56.     Temp := Fibb_Calc( Temp );
  57.     My_Int_IO.PUT( Item => Temp );
  58. END Fibbinochi;
  59.     
  60.